<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>CseSearch</title>

<%@ page import="com.search.QueryAnalyzer"%>
<%@ page import="com.search.QueryResult"%>
<%@ page import="com.data.JDoc"%>
<%@ page import="java.util.*"%>
<%@ page import="java.util.Map.Entry"%>

<%!QueryAnalyzer queryAnalyzer = null;
	QueryResult queryResult = null;%>

</head>

<body>

	<%
		String query = request.getParameter("query");
		if (QueryAnalyzer.isEmpty(query)) {
			query = "";
			String stopWordPath = getServletContext().getRealPath(
					"/data/stopwords.txt");
			String dbPath = getServletContext().getRealPath("/data/web.db");
			queryAnalyzer = new QueryAnalyzer(stopWordPath, dbPath);
			queryResult = null;

		} else {

			if (queryAnalyzer != null) {
				queryAnalyzer.cleanResult();
				queryResult = queryAnalyzer.query(query);
			}
			if (query.contains("\"")) {
				query = query.replace("\"", "&quot;");
			}
		}
	%>
	<table>
		<tr>
			<td>
				<h3>COMP4321 Term Project</h3>
			</td>
		</tr>
		<tr>
			<td>
				<form action="index.jsp" method="post">
					Query : <input type="text" name="query" size="80"
						value="<%=query%>"> <input id="submit" type="submit"
						value="Search" />
				</form>
			</td>
		</tr>
		<tr>
			<td>
				<hr>
			</td>
		</tr>
		<tr>
			<td>Number of results found : <%
				if (queryResult != null) {
					int size = queryResult.getRanking().size();
					if (size > 50) {
						out.print(50);
					} else {
						out.print(size);
					}

				} else {
					out.print(0);
				}
			%>
			</td>
		</tr>
		<tr>
			<td>
				<hr>
			</td>
		</tr>
		<tr>
			<td>
				<table cellpadding="7">
					<%
						if (queryResult != null) {
							ArrayList<Integer> docIdList = queryResult.getRanking();
							int i = 50;
							for (Integer docId : docIdList) {
								Float score = queryResult.getScores().get(docId);
								JDoc doc = queryResult.getResult().get(docId);
					%>

					<tr>
						<td width="100"><%=score%></td>
						<td><a href="<%=doc.getUrl()%>" style="text-decoration: none"><%=doc.getTitle()%>
								<br> <%=doc.getUrl()%> </a></td>
					</tr>
					<tr>
						<td></td>
						<td><%=doc.getLastModified()%>, size: <%=doc.getSize()%> <br>
							<%
								TreeMap<String, Integer> words = queryAnalyzer
												.sortHashMap(doc.getWords());
										int k = 5;
										for (Entry<String, Integer> entry : words.entrySet()) {
											String key = entry.getKey();
											Integer value = entry.getValue();

											out.print("(" + key + ", " + value + ") ");
											k--;
											if (k <= 0)
												break;
										}
							%> <br> parent urls:<%
 	ArrayList<String> parentUrls = doc.getParentUrls();
 			int maxLink = 10;
 			for (String parent : parentUrls) {
 				out.println(" <br> " + parent);
 				maxLink--;
 				if (maxLink <= 0) {
 					int size = parentUrls.size() - 10;
 					out.println("<br>( There are " + size
 							+ " remaining ... )");
 					break;
 				}
 			}
 %> <br> child links:<%
 	ArrayList<String> childLinks = doc.getChildLinks();
 			maxLink = 10;
 			for (String child : childLinks) {
 				out.println(" <br> " + child);
 				maxLink--;
 				if (maxLink <= 0) {
 					int size = parentUrls.size() - 10;
 					out.println("<br>( There are " + size
 							+ " remaining ... )");
 					break;
 				}
 			}
 %></td>
					</tr>
					<%
						i--;
								if (i <= 0)
									break;
							}
						}
					%>
				</table>
			</td>
		</tr>
	</table>
</body>
</html>